package sifaV2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sifaV2.domain.DataDto;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ==={"以危险方法危害公共安全罪":{"categoryIds":"12_24_120_16_34_3_78_36_","num":12},"伪造公司、企业、事业单位、人民团体印章罪":{"categoryIds":"15_12_14_6_3_18_30_9_10_7_24_11_17_13_16_36_21_20_22_","num":430},
 * "侵犯著作权罪":{"categoryIds":"3_24_7_48_16_10_36_26_18_15_1_12_22_30_11_9_25_40_14_","num":142},"信用卡诈骗罪":{"categoryIds":"4_6_36_108_10_18_42_24_39_48_84_12_","num":26},"假冒注册商标罪":{"categoryIds":"36_14_42_24_15_8_38_30_12_48_39_51_10_44_18_47_32_22_11_54_16_46_50_52_17_21_27_13_37_20_60_","num":698},"制作、复制、出版、贩卖、传播淫秽物品牟利罪"
 * :{"categoryIds":"24_37_36_42_12_14_20_9_10_48_5_3_120_18_26_126_84_22_28_54_13_38_60_16_30_72_39_44_11_17_15_57_51_27_46_132_","num":432},"受贿罪":{"categoryIds":"6_66_84_12_18_60_36_72_24_9_42_19_48_156_26_20_120_44_45_132_30_70_21_10_13_135_39_62_180_126_81_15_63_78_96_14_53_16_122_74_25_38_54_28_40_114_7_68_144_69_46_150_108_168_51_33_17_27_37_162_11_123_138_90_82_34_43_75_128_41_58_76_166_129_31_86_102_57_121_64_87_49_174_125_61_93_131_","num":3598},"合同诈骗罪":{"categoryIds":"60_84_36_20_22_16_18_14_144_42_70_54_96_72_66_27_168_78_12_24_13_106_120_9_81_150_126_","num":48},"失火罪":{"categoryIds":"18_4_24_36_14_12_3_20_42_7_5_10_48_15_72_66_30_9_11_16_78_22_28_60_47_54_34_45_21_38_40_39_46_26_29_41_84_25_","num":1482},"强迫交易罪":{"categoryIds":"18_14_6_5_15_12_48_7_56_11_10_21_22_9_24_36_20_17_38_54_39_16_44_60_30_33_26_","num":152},"扰乱无线电通讯管理秩序罪":{"categoryIds":"14_18_9_7_11_6_29_15_10_27_12_32_24_3_22_42_","num":77},"投放危险物质罪":{"categoryIds":"48_60_36_42_12_54_39_24_30_108_18_38_","num":140},"抢劫罪":{"categoryIds":"42_18_54_132_36_90_39_84_12_72_56_120_123_60_48_30_126_41_52_38_234_24_40_144_180_28_7_21_27_66_154_58_64_108_93_45_156_96_51_25_37_70_46_65_102_150_43_91_122_57_138_124_125_62_168_71_53_114_29_17_162_97_10_141_49_158_128_86_92_79_111_16_78_19_15_140_63_69_81_173_174_59_88_35_172_76_33_152_55_61_82_13_67_77_115_153_89_178_47_163_135_121_31_118_100_104_75_106_101_131_136_98_87_166_142_134_160_137_130_186_143_113_99_129_139_165_127_164_103_105_148_","num":7504},"拐卖妇女、儿童罪":{"categoryIds":"72_162_36_60_24_48_120_66_78_18_96_108_42_132_12_56_84_26_144_126_69_39_63_30_156_102_14_22_","num":259},"招摇撞骗罪":{"categoryIds":"24_48_12_36_18_5_30_3_20_49_42_27_15_10_72_11_84_51_39_22_38_96_66_54_14_34_31_46_25_32_16_21_60_","num":294},"挪用公款罪":{"categoryIds":"36_60_162_12_66_3_18_30_48_24_7_96_120_14_144_16_126_10_9_132_45_63_108_84_156_72_11_114_13_168_78_70_42_19_17_90_33_102_99_15_38_54_22_61_39_138_75_27_","num":871},"挪用资金罪":{"categoryIds":"21_36_12_60_3_42_48_15_108_18_24_16_30_7_10_11_9_78_45_72_54_26_14_96_37_17_46_105_13_20_84_66_52_39_38_55_22_64_114_27_63_41_102_40_23_83_44_90_33_51_","num":1086},"敲诈勒索罪":{"categoryIds":"46_48_10_36_24_","num":8},"破坏生产经营罪":{"categoryIds":"12_6_8_14_7_10_18_48_1_3_30_24_36_15_9_17_11_16_32_22_43_42_60_19_","num":224},"窝藏、包庇罪":{"categoryIds":"6_12_3_24_8_5_9_10_18_36_20_30_7_1_60_54_15_14_39_42_22_11_48_33_","num":428},"组织、领导传销活动罪":{"categoryIds":"36_15_7_11_18_12_72_20_42_30_24_10_60_48_66_14_27_22_84_34_26_9_78_63_16_17_19_51_39_13_46_21_33_45_28_67_54_32_31_96_25_70_49_","num":760},"聚众斗殴罪":{"categoryIds":"18_26_42_36_48_12_30_54_16_21_38_24_39_32_29_20_40_14_33_31_5_22_","num":76},"虚开发票罪":{"categoryIds":"6_15_12_4_8_10_48_7_24_3_18_11_20_36_9_30_19_16_21_","num":168},"虚开增值税专用发票、用于骗取出口退税、抵扣税款发票罪":{"categoryIds":"36_18_132_42_15_138_4_12_48_120_22_9_54_24_34_14_21_7_84_30_10_28_72_150_96_66_65_108_144_124_16_37_90_13_126_17_60_40_39_114_123_51_180_33_75_58_45_46_156_27_122_174_25_52_19_68_168_102_59_86_","num":1368},"诈骗罪":{"categoryIds":"48_20_15_36_54_47_108_12_50_132_40_17_56_58_10_156_18_104_24_27_19_44_126_42_41_43_63_72_180_14_66_30_45_60_84_120_16_62_144_114_22_86_150_93_39_81_46_51_78_29_138_55_162_70_102_99_135_37_96_168_101_90_31_74_151_11_160_28_52_82_33_79_34_21_123_128_53_174_64_69_91_124_152_13_25_105_57_59_83_75_136_49_149_134_92_173_88_89_67_159_122_117_94_65_115_76_222_61_100_113_170_111_169_153_130_106_139_77_175_140_129_116_178_148_186_143_147_103_165_87_98_133_146_127_71_121_154_107_137_110_142_118_198_125_164_167_176_155_177_141_145_166_112_191_171_161_85_158_172_95_157_","num":19299},"贪污罪":{"categoryIds":"1_120_","num":2},"走私、贩卖、运输、制造毒品罪":{"categoryIds":"6_24_42_84_48_180_5_12_114_108_9_106_102_132_198_36_7_90_40_30_54_44_10_15_94_120_19_56_22_39_61_38_18_60_41_111_58_43_51_112_27_37_96_78_156_65_144_16_168_46_45_87_52_69_50_105_13_70_72_28_113_29_33_88_160_76_21_138_57_75_162_17_118_34_116_64_74_129_86_165_47_26_151_79_66_124_150_131_159_23_99_126_100_53_63_92_25_184_55_91_174_49_134_110_85_117_97_149_71_101_103_93_95_135_104_67_178_141_136_152_167_142_89_107_115_200_163_166_82_158_125_153_139_130_186_123_122_148_109_119_172_154_77_81_183_170_127_140_161_190_128_176_121_73_157_","num":29385},"走私普通货物、物品罪":{"categoryIds":"51_12_16_24_72_42_3_14_36_30_48_132_18_11_60_144_10_7_120_180_9_84_5_156_96_90_66_21_39_27_168_15_81_22_54_","num":374},"过失致人死亡罪":{"categoryIds":"36_24_10_18_12_42_30_28_7_60_72_9_17_48_14_21_58_33_84_66_15_54_78_40_16_51_27_39_20_11_22_44_32_26_38_46_180_29_19_","num":1776},"过失致人重伤罪":{"categoryIds":"15_24_12_6_10_18_11_9_16_30_36_13_32_7_14_22_20_19_","num":322},"违法发放贷款罪":{"categoryIds":"24_18_30_10_36_48_12_20_27_60_90_54_3_15_63_84_14_9_","num":82},"重大劳动安全事故罪":{"categoryIds":"18_12_36_10_24_9_7_20_16_14_5_60_","num":121},"重婚罪":{"categoryIds":"8_2_20_24_12_9_6_5_10_18_7_11_3_14_15_","num":180},"非国家工作人员受贿罪":{"categoryIds":"12_14_","num":2},"非法制造、买卖、运输、邮寄、储存枪支、弹药、爆炸物罪":{"categoryIds":"36_120_18_48_54_39_84_126_15_30_12_24_42_144_132_38_22_10_66_60_156_72_28_27_21_37_46_58_14_78_128_138_40_29_135_130_41_96_168_90_45_16_25_17_180_34_123_11_","num":1772},"非法占用农用地罪":{"categoryIds":"12_36_14_7_8_5_24_27_30_18_3_10_9_1_13_16_22_26_20_17_15_42_48_28_21_19_11_25_54_","num":1648},"非法处置查封、扣押、冻结的财产罪":{"categoryIds":"8_6_7_3_18_30_12_11_10_4_22_5_9_36_24_15_14_16_","num":152},
 * "非法持有、私藏枪支、弹药罪":{"categoryIds":"12_10_6_5_36_8_7_9_24_15_18_45_1_14_3_30_31_28_13_48_39_16_42_29_21_54_","num":381}
 *
 * ,"非法生产、买卖、运输制毒物品、走私制毒物品罪":{"categoryIds":"10_20_7_90_36_120_30_9_54_51_8_38_18_58_84_46_48_24_102_42_39_57_12_26_144_96_15_108_66_60_16_","num":84},"非法经营罪":{"categoryIds":"12_6_10_8_72_36_84_7_15_30_18_24_60_3_11_63_66_13_45_62_9_42_32_82_39_78_27_33_54_48_26_75_17_156_108_14_20_64_16_96_19_21_22_28_46_29_31_40_44_51_61_144_68_25_70_90_67_","num":2577},"非法转让、倒卖土地使用权罪":{"categoryIds":"36_18_24_30_13_48_10_9_12_54_15_7_22_40_42_38_27_51_60_","num":144}}
 * ==={"串通投标罪":{"categoryIds":"","num":56},"保险诈骗罪":{"categoryIds":"","num":2},"出售、购买、运输假币罪":{"categoryIds":"","num":80},"单位行贿罪":{"categoryIds":"","num":2},"持有、使用假币罪":{"categoryIds":"","num":94},"持有伪造的发票罪":{"categoryIds":"","num":100},"盗掘古文化遗址、古墓葬罪":{"categoryIds":"","num":128},"绑架罪":{"categoryIds":"","num":197},"贷款诈骗罪":{"categoryIds":"","num":8},"非法持有、私藏枪支、弹药罪":{"categoryIds":"","num":3718},"非法持有毒品罪":{"categoryIds":"","num":3597}}
   结论，相同的罪名，金额等指标不一样，则程度不一样。

 */
public class DataAnaly {
    /**
     * ==={"以危险方法危害公共安全罪":12,"伪造公司、企业、事业单位、人民团体印章罪":430,"侵犯著作权罪":142,"信用卡诈骗罪":26,"假冒注册商标罪":698,
     * "制作、复制、出版、贩卖、传播淫秽物品牟利罪":432,"受贿罪":3598,"合同诈骗罪":48,"失火罪":1482,"强迫交易罪":152,
     * "扰乱无线电通讯管理秩序罪":77,"投放危险物质罪":140,"抢劫罪":7504,"拐卖妇女、儿童罪":259,"招摇撞骗罪":294,
     * "挪用公款罪":871,"挪用资金罪":1086,"敲诈勒索罪":8,"破坏生产经营罪":224,"窝藏、包庇罪":428
     * ,"组织、领导传销活动罪":760,"聚众斗殴罪":76,"虚开发票罪":168,"虚开增值税专用发票、用于骗取出口退税、抵扣税款发票罪":
     * 1368,"诈骗罪":19299,"贪污罪":2,"走私、贩卖、运输、制造毒品罪":29385,"走私普通货物、物品罪":374,
     * "过失致人死亡罪":1776,"过失致人重伤罪":322,"违法发放贷款罪":82,"重大劳动安全事故罪":121,"重婚罪":180,
     * "非国家工作人员受贿罪":2,"非法制造、买卖、运输、邮寄、储存枪支、弹药、爆炸物罪":1772,"非法占用农用地罪":1648,
     * "非法处置查封、扣押、冻结的财产罪":152,
     * <p>
     * <p>
     * ======"非法持有、私藏枪支、弹药罪":381==12..21..3..29...
     * <p>
     * <p>
     * ,"非法生产、买卖、运输制毒物品、走私制毒物品罪":84
     * ,"非法经营罪":2577,"非法转让、倒卖土地使用权罪":144}
     * .............
     * ==={"串通投标罪":56,"保险诈骗罪":2,"出售、购买、运输假币罪":80,"单位行贿罪":2,"持有、使用假币罪":94,"持有伪造的发票罪":100,
     * "盗掘古文化遗址、古墓葬罪":128,"绑架罪":197,"贷款诈骗罪":8,
     * <p>
     * <p>
     * "非法持有、私藏枪支、弹药罪":3718,"非法持有毒品罪":3597}
     *
     * @param args
     */
    public static void main(String args[]) {
        getTrain();
        getTest();
    }

    private static void getTest() {
        String path = "/Users/wengjunjie/works/人工智能/11data/1632652922606/bisai.txt";
        List<String> scanListPath = readFile02(path);
        Map<String, DataDto> map = new HashMap<>();
        int i = 0;
        for (String s : scanListPath) {
            s = replaceHtml(s);
            JSONObject jsonObject = JSON.parseObject(s);
            int filename = jsonObject.getInteger("filename");
            String caseCause = jsonObject.getString("caseCause");
            String justice = jsonObject.getString("justice");
            // System.out.println(i++ + "..caseCause=" + justice);
            String opinion = jsonObject.getString("opinion");
            String province = jsonObject.getString("province");
            DataDto dto = map.get(caseCause);
            if (dto == null) {
                dto = new DataDto();
            }
            if (dto.getNum() == null) {
                dto.setNum(1);
            } else {
                dto.setNum(1 + dto.getNum());
            }
            map.put(caseCause, dto);
        }
        System.out.println("===" + JSONObject.toJSONString(map));

    }

    private static void getTrain() {
        // /Users/wengjunjie/works/人工智能/11data/1632652922606/刑期预测赛道一期数据_训练
        String path = "/Users/wengjunjie/works/人工智能/11data/1632652922606/train.txt";
        Map<String, DataDto> map = new HashMap<>();
        int i = 0;
        List<String> scanListPath = readFile02(path);
        for (String s : scanListPath) {

            s = replaceHtml(s);
            JSONObject jsonObject = JSON.parseObject(s);
            int filename = jsonObject.getInteger("filename");
            String caseCause = jsonObject.getString("caseCause");
            String justice = jsonObject.getString("justice");
            String opinion = jsonObject.getString("opinion");
            String province = jsonObject.getString("province");
            String judge = jsonObject.getString("judge");
            DataDto dto = map.get(caseCause);
            if (dto == null) {
                dto = new DataDto();
            }
            if (dto.getNum() == null) {
                dto.setNum(1);
            } else {
                dto.setNum(1 + dto.getNum());
            }
            if (!dto.getCategoryIds().contains(judge + "_")) {
                dto.setCategoryIds(dto.getCategoryIds() + judge + "_");
            }
            map.put(caseCause, dto);
        }
        System.out.println("===" + JSONObject.toJSONString(map));
    }


    private static String replaceHtml(String s) {
        //{"caseCause": "非法经营罪", "justice": "经审理查明，2011年至2013年，王×担任VICTORYMARKETINGLIMITED大中华区市场部总监、北京中夏融通国际教育咨询有限公司（办公地点为：北京市朝阳区建外SOHO16号楼1706号）负责人，让客户下载并注册未经批准的"VFX"平台，并以此经营炒黄金、外汇期货的业务，金额达人民币30万余元。被告人王×后被抓获归案。\n2013年8月8日，梁XX的投资款是124000人民币（2万美元），当时只汇了15400美元至王×香港汇丰银行卡上（×××），剩下的钱用于员工发工资了。王×给了我一个平台卡号（711026），让我进行交易。\n2013年11月27日，徐×通过建设银行投入20300美金至王×建设银行。2014年1月，徐×要求终止交易，王×说平台上的钱他自己用了，无法还给我和徐×。王×给我们打了一个欠条。\n2013年11月29日至2014年1月20日，户名为"jinhaixu"的客户在"VFXTraher"系统的操作情况，买入了"gbpusd、xauusd、eurusd、audusd、"。\n2012年5月26日，王×在香港汇丰银行以自己名义开立的"VictoryMarketingLtd"的账户（×××）资金往来情况，其中2013年8月9日存入美元15391.6元，2013年11月27日存入美元19984.06元。\n2011年7月左右，倪×让我在新西兰注册"VFX奥X国际"国际炒汇、炒黄金平台，网址"www。vmarkets。net"。VFX奥X国际的网站是倪×找人设计的。奥盛国际公司大中华区市场部总监的聘书是倪×给我的。倪×给我们一个监管账户可以看到客户购买的产品。中夏融通公司和元丰投资、营口百信都是VFX奥X国际的代理商。该平台能够链接FXDD马耳他交易商和LAMAX英国交易所。\n",
        // "opinion": "本院认为，被告人王×法制观念淡薄，违反国家规定，未经国家有关主管部门批准非法经营买卖黄金、外汇的期货业务，其行为已构成非法经营罪，依法应予惩处。北京市朝阳区人民检察院指控被告人王×犯非法经营罪的事实清楚，证据确实、充分，指控的罪名成立；唯指控的犯罪数额有误，本院根据现有证据依法予以变更。被告人王×关于自己没有非法经营的辩护意见，及其辩护人关于本案证据不足的辩护意见，缺乏事实依据，本院不予采纳。综上，根据被告人王×犯罪的事实、犯罪的性质、情节以及对于社会的危害程度，本院依照《中华人民共和国刑法》第二百二十五条、第六十一条、第四十五条、第四十七条、第五十二条、第五十三条之规定，",
        // "province": "北京市", "judge": 6, "filename": "18430542"}
        String tmp = s.split("justice")[1];
        String justice = tmp.split("opinion")[0];
        justice = justice.substring(4, justice.length() - 4);
        String justice2 = justice.replaceAll("\"", "");
        justice2 = justice2.replaceAll("\\\\", "");
        s = s.replace(justice, justice2);

        tmp = s.split("opinion")[1];
        String opinion = tmp.split("province")[0];
        opinion = opinion.substring(4, opinion.length() - 4);
        String opinion2 = opinion.replaceAll("\"", "");
        opinion2 = opinion2.replaceAll("\\\\", "");
        s = s.replace(opinion, opinion2);

        JSONObject json = JSONObject.parseObject(s);
        return s;
    }

    /**
     * 读取一个文本 一行一行读取
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static List<String> readFile02(String path) {
        // 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
        List<String> list = new ArrayList<String>();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 防止路径乱码   如果utf-8 乱码  改GBK     eclipse里创建的txt  用UTF-8，在电脑上自己创建的txt  用GBK
        InputStreamReader isr = null;
        try {
            isr = new InputStreamReader(fis, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while (true) {
            try {
                if (!((line = br.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 如果 t x t文件里的路径 不包含---字符串       这里是对里面的内容进行一个筛选
            if (line.lastIndexOf("---") < 0) {
                list.add(line);
            }
        }
        try {
            br.close();
            isr.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }


}
